package org.samith.dao;

import java.util.Collection;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import javax.xml.registry.infomodel.User;

import org.samith.entity.ServiceType;
import org.samith.entity.SubscribedService;
import org.samith.entity.Subscriber;

@Stateless
public class SubscribedServiceDaoImpl extends BaseDaoImpl<SubscribedService>
		implements SubscribedServiceDao {

	@EJB
	UserDao userDao;

	@EJB
	ServiceTypeDao serviceTypeDao;

	@EJB
	SubscriberDao subscriberDao;

	@Override
	public Class<SubscribedService> getType() {
		return SubscribedService.class;
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<SubscribedService> findSubscribedServicesBySubscriberId(
			Long subscriberId) {
		Query query = em
				.createQuery("SELECT a FROM SubscribedService a WHERE a.subscriberId = :s ");
		Subscriber subscriber = em.find(Subscriber.class, subscriberId);
		System.out
				.println("=========== subscriber in findSubscribedServicesBySubscriberId "
						+ subscriber);
		query.setParameter("s", subscriber);
		Collection<SubscribedService> subscribedServices = query
				.getResultList();
		return subscribedServices;
	}

	@Override
	public Collection<SubscribedService> findSubscribedServicesByUserId(
			Long userId) {
		Query query = em
				.createQuery("SELECT a FROM Subscriber a WHERE a.userId = :u ");
		User usr = (User) userDao.findById(userId);
		query.setParameter("u", usr);
		Subscriber subscriber;
		try {
			subscriber = (Subscriber) query.getSingleResult();
			if (subscriber != null)
				return findSubscribedServicesBySubscriberId(subscriber.getId());
			else
				return null;
		} catch (NoResultException ex) {
			return null;
		}
	}

	@Override
	public boolean hasSubscribedToService(ServiceType serviceType,
			Subscriber subscriber) {
		if (serviceType != null && subscriber != null) {
			Query query = em
					.createQuery("SELECT a FROM SubscribedService a WHERE a.subscriberId = :u AND a.serviceId = :s ");
			ServiceType serviceTypeObj = serviceTypeDao.read(serviceType
					.getId());
			Subscriber subscriberObj = subscriberDao.read(subscriber.getId());
			query.setParameter("s", serviceTypeObj);
			query.setParameter("u", subscriberObj);
			SubscribedService subscribedService = null;
			try {
				subscribedService = (SubscribedService) query.getSingleResult();
			} catch (NoResultException ex) {
			}
			if (subscribedService != null) {
				return true;
			}
			return false;
		}
		return true;
	}
	
	@Override
	public SubscribedService getSubscribedToServiceRecord(ServiceType serviceType,
			Subscriber subscriber) {
		SubscribedService subscribedService = null;
		if (serviceType != null && subscriber != null) {
			Query query = em
					.createQuery("SELECT a FROM SubscribedService a WHERE a.subscriberId = :u AND a.serviceId = :s ");
			ServiceType serviceTypeObj = serviceTypeDao.read(serviceType
					.getId());
			Subscriber subscriberObj = subscriberDao.read(subscriber.getId());
			query.setParameter("s", serviceTypeObj);
			query.setParameter("u", subscriberObj);
			try {
				subscribedService = (SubscribedService) query.getSingleResult();
			} catch (NoResultException ex) {
			}
			if (subscribedService != null) {
				return subscribedService;
			}
			return null;
		}
		return null;
	}
}